#!/bin/bash

# Linux 服务器中文字体安装脚本
# 用于解决 PDF 转换中文乱码问题

echo "开始安装中文字体..."

# 检测系统类型
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
    OS=$(lsb_release -si)
    VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    OS=Debian
    VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
    OS=openSUSE
elif [ -f /etc/redhat-release ]; then
    OS=RedHat
else
    OS=$(uname -s)
    VER=$(uname -r)
fi

echo "检测到系统: $OS $VER"

# 创建字体目录
sudo mkdir -p /usr/share/fonts/chinese
cd /usr/share/fonts/chinese

# 根据系统类型安装字体
if [[ "$OS" == *"Ubuntu"* ]] || [[ "$OS" == *"Debian"* ]]; then
    echo "Ubuntu/Debian 系统，使用 apt 安装字体..."

    # 更新包列表
    sudo apt update

    # 安装中文字体包
    sudo apt install -y fonts-wqy-microhei fonts-wqy-zenhei
    sudo apt install -y fonts-noto-cjk fonts-noto-cjk-extra
    sudo apt install -y fonts-arphic-ukai fonts-arphic-uming
    sudo apt install -y fonts-droid-fallback

    # 安装 fontconfig 工具
    sudo apt install -y fontconfig

elif [[ "$OS" == *"CentOS"* ]] || [[ "$OS" == *"Red Hat"* ]] || [[ "$OS" == *"Rocky"* ]]; then
    echo "CentOS/RHEL 系统，使用 yum/dnf 安装字体..."

    # 检查是否有 dnf
    if command -v dnf &> /dev/null; then
        PKG_MANAGER="dnf"
    else
        PKG_MANAGER="yum"
    fi

    # 安装 EPEL 仓库（如果需要）
    sudo $PKG_MANAGER install -y epel-release

    # 安装中文字体包
    sudo $PKG_MANAGER install -y wqy-microhei-fonts wqy-zenhei-fonts
    sudo $PKG_MANAGER install -y google-noto-sans-cjk-fonts
    sudo $PKG_MANAGER install -y fontconfig

else
    echo "其他系统，尝试手动下载字体..."

    # 下载 Google Noto 字体
    echo "下载 Google Noto Sans CJK SC 字体..."
    wget -O NotoSansCJKsc-Regular.otf "https://github.com/googlefonts/noto-cjk/raw/main/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Regular.otf"

    # 下载文泉驿字体
    echo "下载文泉驿微米黑字体..."
    wget -O wqy-microhei.ttc "https://sourceforge.net/projects/wqy/files/wqy-microhei/0.2.0-beta/wqy-microhei-0.2.0-beta.tar.gz/download"
    if [ -f wqy-microhei-0.2.0-beta.tar.gz ]; then
        tar -xzf wqy-microhei-0.2.0-beta.tar.gz
        cp wqy-microhei/wqy-microhei.ttc ./
        rm -rf wqy-microhei wqy-microhei-0.2.0-beta.tar.gz
    fi
fi

# 手动下载一些常用字体作为备份
echo "下载额外的中文字体..."

# 下载思源黑体
if [ ! -f "SourceHanSansCN-Regular.otf" ]; then
    echo "下载思源黑体..."
    wget -O SourceHanSansCN-Regular.otf "https://github.com/adobe-fonts/source-han-sans/raw/release/OTF/SimplifiedChinese/SourceHanSansCN-Regular.otf" 2>/dev/null || echo "思源黑体下载失败，跳过"
fi

# 设置字体权限
sudo chmod 644 /usr/share/fonts/chinese/*

# 更新字体缓存
echo "更新字体缓存..."
sudo fc-cache -fv

# 检查安装的中文字体
echo "检查已安装的中文字体:"
fc-list :lang=zh | head -10

echo "字体安装完成！"

# 验证字体是否可用
echo "验证常用中文字体是否可用:"
fonts_to_check=("Noto Sans CJK SC" "WenQuanYi Micro Hei" "Source Han Sans CN" "WenQuanYi Zen Hei")

for font in "${fonts_to_check[@]}"; do
    if fc-list | grep -qi "$font"; then
        echo "✓ $font - 可用"
    else
        echo "✗ $font - 不可用"
    fi
done

echo ""
echo "安装完成！现在可以重新运行 PDF 转换程序。"
echo "如果仍然有乱码问题，请检查日志中的字体检测信息。"
